package com.wlm.service.impl;

import com.wlm.Utils.TimeUtitl;
import com.wlm.dao.FormDAO;
import com.wlm.dto.rep.FormDto;
import com.wlm.entity.Form;
import com.wlm.service.FormService;
import io.swagger.models.auth.In;
import net.bytebuddy.implementation.MethodDelegation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class FormServiceImpl implements FormService {

    @Autowired
    private FormDAO formDAO;

    @Override
    public Integer addForm(FormDto formDto) {
        Integer id = formDto.getId();
        List<String> types = formDto.getType();
        Form form = new Form();
        BeanUtils.copyProperties(formDto, form);
        if (types != null) {
            String type = String.join(",", types);
            form.setType(type);
        }
        Boolean delivery = formDto.getDelivery();
        if (delivery != null && delivery) {
//            form.setDelivery()
        }
        form.setDate1(TimeUtitl.dateTime());
        form.setDate2(TimeUtitl.dateTime());
        if (id == null) {
            return formDAO.addForm(form);
        } else {
            return formDAO.updateForm(form);
        }
    }

    // org.apache.commons.lang3
    // ObjectUtils   StringUtils
    //一种是org.apache.commons.lang3包下的StringUtils.isEmpty(CharSequence cs);
    // org.apache.commons.lang3包下的StringUtils类，判断是否为空的方法参数是字符序列类，也就是String类型

    @Override
    public FormDto queryForm(FormDto formDto) {
//        ObjectUtils
        Form form = formDAO.queryForm(formDto);
        FormDto dto = new FormDto();
        BeanUtils.copyProperties(form, dto);
        String types = form.getType();
        // 判断字符串是否为空 字符串以逗号分割，转换字符串数组
        if (!StringUtils.isEmpty(types)) {
            dto.setType(Arrays.asList(types.split(",")));
        }
        return dto;
    }
}
